"""
    通讯录页面
"""
import logging
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class ContactsPage:

    def __init__(self, driver):
        self.driver = driver

    def add_member(self, username, account, mobile):
        """添加成员"""
        # WebDriverWait(self.driver, 5).until(self.click_add_member())
        # WebDriverWait(self.driver, 5).until(
        #     expected_conditions.invisibility_of_element((".ww_operationBar .js_add_member")))
        # 点击通讯录

        # self.driver.find_elements(By.CSS_SELECTOR, ".ww_operationBar .js_add_member")[0].click()
        sleep(1)
        WebDriverWait(self.driver, 5).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, ".ww_operationBar .js_add_member"))).click()
        # 输入 姓名
        self.driver.find_element(By.NAME, "username").send_keys(username)
        # 输入 账号
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_acctid").send_keys(account)
        # 输入 手机号
        self.driver.find_element(By.NAME, "mobile").send_keys(mobile)
        # 点击 确认
        self.driver.find_element(By.CSS_SELECTOR, ".js_btn_save").click()
        # 点击 取消
        # self.driver.find_element(By.CSS_SELECTOR, ".js_btn_cancel").click()
        sleep(1)
        return self.search_member(username)
        # ele = WebDriverWait(self.driver,60).until(self.find_member((By.CSS_SELECTOR,".js_next_page"),(By.CSS_SELECTOR,f"[title={username}]")))
        # return ele.text

    def edit_member(self, username, reusername, remobile):
        """编辑"""
        text = self.search_member(username)
        if text is None:
            raise Exception(f"执行编辑操作时，搜索的成员{username}不存在!!!")
        # 点击编辑按钮
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, ".js_edit").click()
        self.driver.find_element(By.NAME, "username").clear()
        self.driver.find_element(By.NAME, "username").send_keys(reusername)
        self.driver.find_element(By.NAME, "mobile").clear()
        self.driver.find_element(By.NAME, "mobile").send_keys(remobile)
        # self.driver.save_screenshot(f"./img/{reusername}img.png")
        # 点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR, ".js_save").click()
        return self.driver.find_element(By.CSS_SELECTOR, ".member_display_cover_detail_name").text

    def delete_member(self, username):
        """删除用户信息"""
        text = self.search_member(username)
        if text is None:
            raise Exception(f"执行删除操作时，搜索的成员{username}不存在!!!")
        # 点击编辑按钮
        self.driver.find_element(By.CSS_SELECTOR, ".js_del_member").click()
        self.driver.find_element(By.CSS_SELECTOR, ".ww_btn_Blue").click()
        # 等待后台删除操作 一秒
        sleep(1)
        return self.search_member(username)

    def search_member(self, username):
        """搜索成员"""
        self.driver.find_element(By.CSS_SELECTOR, ".ww_searchInput_text").clear()
        self.driver.find_element(By.CSS_SELECTOR, ".ww_searchInput_text").send_keys(username)
        li_eles = self.driver.find_elements(By.CSS_SELECTOR, "#search_member_list>li")
        if len(li_eles) == 0:
            logging.info(f"搜索的成员{username}不存在!!!")
            return None
        for li in li_eles:
            try:
                li.click()
                # 获取 搜索的用户名称
                text = self.driver.find_element(By.CSS_SELECTOR, ".member_display_cover_detail_name").text
                if text == username:
                    return text
            except Exception as e:
                logging.error(f"搜索的成员{username}不存在!!!")
                logging.error(e)
        logging.error(f"搜索的成员{username}不存在!!!")
        raise Exception(f"搜索的成员{username}不存在!!!")

    def return_contacts_page(self):
        self.driver.find_element(By.CSS_SELECTOR, "#clearMemberSearchInput").click()

    def find_member(self, element_click, expected_element):
        def _inner(driver):
            sleep(1)
            try:
                ele = driver.find_element(*expected_element)
            except:
                ele = None
            self.driver.find_element(*element_click).click()
            return ele

        return _inner
